package com.example.zjhuojback.service;

import com.example.zjhuojback.entity.Submission;
import com.example.zjhuojback.entity.User;
import com.example.zjhuojback.mapper.SubmissionMapper;
import com.example.zjhuojback.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class SubmissionService {
    @Autowired
    SubmissionMapper submissionMapper;
    @Autowired
    UserMapper userMapper;
    // 保存提交记录#{username}, #{name}, #{problemId}, #{result}, #{memoryUsage}, #{timeUsage}, #{language})")
    public void saveSubmission(String username, String name, Integer problemId, String result,
                               Integer memoryUsage, Integer timeUsage, String language, String code, LocalDateTime submissionTime) {
        Submission submission = new Submission();
        submission.setUsername(username);
        submission.setName(name);
        submission.setProblemId(problemId);
        submission.setResult(result);
        submission.setMemoryUsage(memoryUsage);
        submission.setTimeUsage(timeUsage);
        submission.setLanguage(language);
        submission.setSubmissionTime(submissionTime);
        submission.setCode(code);
        submissionMapper.insert(submission);
    }

    // 获取所有提交记录（分页），并且支持搜索条件
    public List<Submission> getPaginatedSubmissions(int pageNum, int pageSize, Submission submission, int userId) {
        int offset = (pageNum - 1) * pageSize;
        User user =  userMapper.selectById(userId);
        if (Objects.equals(user.getPermission(), "-1")) submission.setUsername(user.getUsername());
        return submissionMapper.selectSubmissionsWithPagination(offset, pageSize, submission);
    }


    // 获取提交记录总数
    public int getSubmissionCount() {
        return submissionMapper.getSubmissionCount();
    }


}
